<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../node_modules/layui/src/css/layui.css">
    <link rel="stylesheet" href="../css/notice.css">
    <script src="../node_modules/layui/src/layui.js"></script>
</head>
<body style="overflow-x: hidden;">

    <div class="layui-row noticeDiv">
        <a href="javascript:;" class="noticeFont">新建公告</a>
    </div>

    <div class="layui-fiuld layui-row" style="background-color: gainsboro;">
        <div class="layui-container layui-col-md-offset1 containerDiv">
            <div class="containerDiv2">
                <div class="baseInfoDiv">
                    <span style="font-weight:bolder">基本信息</span>
                </div>

                <form  class="layui-form">
                    <div class="layui-form-item">
                        <div class="formDiv1">
                            <label class="layui-form-label">名称
                                <span>*</span>
                            </label>
                        </div>
                        <div class="layui-input-block formInputDiv">
                            <input id="testInput" type="text" name="notice_name" required  lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
                        </div>

                        <div class="grayFontDiv">
                            <span>
                                100个字符以内，支持汉字、英文、标点符号、数字、不含转义符
                            </span>
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <div class="formDiv2">
                            <label class="layui-form-label">描述</label>
                        </div>
                        <div class="layui-input-block formTextAreaDiv2">
                          <textarea placeholder="请输入内容" class="layui-textarea" name="notice_des"></textarea>
                        </div>

                        <div class="grayFontDiv">
                            <span>
                                100个字符以内，支持汉字、英文、标点符号、数字、不含转义符
                            </span>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="formDiv2">
                            <label class="layui-form-label">时区</label>
                        </div>
                        <div class="layui-input-block formInputDiv">
                            <select name="notice_timeZone" lay-verify="required">
                              <option value="0">UTC+8 东八区（北京时间）</option>
                              <option value="1">上海</option>
                              <option value="2">广州</option>
                              <option value="3">深圳</option>
                              <option value="4">杭州</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="formDiv4">
                            <label class="layui-form-label">有效期
                                <span>*</span>
                            </label>
                        </div>
                      
                        <div class="layui-inline formLayuiDate" id="test6">
                            <div class="layui-input-inline">
                                <input type="text" autocomplete="off" id="test-startDate-1" class="layui-input" placeholder="开始日期">
                            </div>
                            <div class="layui-form-mid">
                                ~
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" autocomplete="off" id="test-endDate-1" class="layui-input" placeholder="结束日期">
                            </div>
                        </div>

                        <div class="grayFontDiv">
                            <span>
                                有效期为必填项目
                            </span>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="formDiv5">
                            <label class="layui-form-label">信息图片</label>
                        </div>
                        <div class="layui-upload formDivUpload">
                            <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                              预览图：
                              <div class="layui-upload-list" id="demo2"></div>
                           </blockquote>
                           <button type="button" class="layui-btn" id="test2">多图片选择</button> 
                        </div>

                        <div class="grayFontDiv">
                            <span>
                                请上传jpg、png、jpeg格式的图片，大小在2MB以内
                            </span>
                        </div>
                    </div>

                    <div class="noticeDefineFont layui-inline">
                        <span>公告自定义属性</span>
                    </div>

                  
                    <div class="layui-form-item">
                        <table class="layui-hide" id="editTable" lay-filter="test"></table>
                        <p style="text-align:center">
                            <button type="button" id="addQuality" class="layui-btn layui-btn-radius layui-btn-normal" data-type="auto">
                                添加属性
                            </button>
                        </p>
                    </div>

                    <div class="layui-form-item">
                        <div>
                            <p style="text-align:center">
                                <button id="saveButton" type="button" lay-submit lay-filter="saveNotice" class="layui-btn layui-btn-fluid layui-btn-warm">保存</button>
                            </p>
                        </div>
                    </div>
                    
                </form>
            </div>
        </div>
    </div>
    <button id="hidden" style="display: none;">aaaaaaaaaaaaaaa</button>    
    



    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>


    <script type="text/javascript">
        layui.use(['element','form','laydate','upload','table','layer'],function(){
            var $ = layui.$,
            element = layui.element,
            form = layui.form,
            laydate = layui.laydate,
            upload = layui.upload,
            table = layui.table,
            layer = layui.layer;


            element.init();


            var addQualityIndex = 0;
            $("#addQuality").click(function(){
                addQualityIndex = layer.open({
                    type:2,
                    title:'添加属性',
                    content:'addQuality.html',
                    area:['500px','500px']
                });
            });
        
            var notice_startEndTime;
            laydate.render({
                elem: '#test6'
                ,range: ['#test-startDate-1', '#test-endDate-1']
                ,type:'datetime'
                ,done:function(value,date){
                    notice_startEndTime = value;
                }
            });
            var pic = false;
            upload.render({
                elem: '#test2'
                ,url: '' //改成您自己的上传接口
                ,multiple: true
                ,exts:'png|jpg|jpeg'
                ,size: 2048
                ,auto: false
                ,bindAction: '#hidden'
                ,choose: function(obj){
                        pic = true; 
                        //预读本地文件示例，不支持ie8
                        obj.preview(function(index, file, result){
                            $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
                        });
                }
                ,before: function(obj){
                
                }
                ,done: function(res){
                    //上传完毕
                    console.log("上传")
                }
            });

            table.render({
                elem:'#editTable',
                data:[],
                cols: [
                    [
                        {field:'notice_key',edit:"text",align:"center",title:'key'},
                        {field: 'notice_value', edit: "text", align: "center", title: 'value'},
                        {fixed: 'right', title:'操作', toolbar: '#barDemo', align: "center"}
                    ]
                ]
            });
            var cbList = [];
            layui.define(function(exports){
                exports('tableAddQuality',function(key,value){
                    var obj = {
                        "notice_key":key,
                        "notice_value":value,
                    };
                    cbList = table.cache.editTable;
                    for(var i = 0;i<cbList.length;i++){
                        if(cbList[i].notice_key === obj.notice_key){
                            layer.close(addQualityIndex);
                            layer.msg('notice_key已存在'); 
                            return;
                        }

                    }
                    cbList.push(obj);
                    table.reload("editTable",{
                        data:cbList,
                    })
                    console.log(cbList);
                    //关闭弹出层
                    layer.close(addQualityIndex);
                });
            });
            
            table.on('tool(test)',function(obj){
                console.log(obj);
                var checkStatus = table.checkStatus(obj.id);
                if(obj.event === 'del'){
                    layer.confirm('真的删除自定义属性么？', function(index){
                        obj.del();
                        layer.close(index);
                    })
                }
            });

            form.on('submit(saveNotice)',function(data){
                if(notice_startEndTime != null){
                    data.field.notice_startEndTime = notice_startEndTime;
                }
                if(cbList.length > 0){
                    data.field.cbList = cbList;
                }
                console.log("保存 ：" + JSON.stringify(data.field));
                if(pic){
                    $('#hidden').click();
                }
                console.log(pic);
                // $.ajax({
                //     url:'',
                //     method:'post',
                //     data:'数据',
                //     dataType:'JSON',
                //     success:function(res){
                //         if(res.code == '0'){
                //             //提交成功
                //             //提交图片
                //         }
                //     }
                // });
            });
        });
    </script>
</body>
</html>